package tom.client;

import java.util.Date;

import com.google.gwt.core.client.*;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.event.dom.client.*;

public class WindMill implements EntryPoint {

	private Button btnBack;
	private Label lbCompany;
	public static Clock clock;
	private static Label lbStatus;

	public void onModuleLoad() {
		Library.getService().getServerTime(new AsyncCallback<Long>() {
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			public void onSuccess(Long result) {
				FlexTable p = new FlexTable();
				p.setWidth("200px");
				clock = new Clock(result);
				RootPanel.get("clockbar").setStyleName("clockbar");
				Label lb = clock.getLabelDate();
				lb.setStyleName("clockbar");
				lb.setWidth("85px");
				p.setWidget(1, 1, lb);
				lb = clock.getLabelTime();
				lb.setStyleName("clockbar");
				lb.setWidth("85px");
				p.setWidget(1, 2, lb);
				RootPanel.get("clockbar").add(p);
			}
		});
		Window.setTitle(Window.Location.getPath());
		FlexTable toolbar = new FlexTable();
		toolbar.setStyleName("toolbar");
		RootPanel.get().setStyleName("desktop");
		// toolbar.setBorderWidth(5);
		// toolbar.setCellSpacing(5);

		RootPanel.get("toolbar").add(toolbar);
		toolbar.setWidget(1, 1, getBtnBack());
		toolbar.setWidget(1, 2, getLbCompany());
		RootPanel.get("loginbar").setStyleName("toolbar");
		RootPanel.get("loginbar").add(FormLogin.frmLogin);
		RootPanel.get("main").add(MainPanel.mainPanel);
		lbStatus = new Label();
		lbStatus.setSize("695px", "25px");
		// lbStatus.setHeight("22px");
		lbStatus.setStyleName("statusbar");
		RootPanel.get("statusbar").add(lbStatus);

		MainPanel.mainPanel.show(MainMenu.menu);
		FormLogin.frmLogin.setWindmill(this);
		Library.getService().getSessVar("user", userCallback);
		Library.getService().getDeployDate(new AsyncCallback<Date>() {
			@Override
			public void onSuccess(Date result) {
				lbStatus.setText("ปรับปรุงล่าสุด "
						+ Library.DATETIME_FORMAT.format(result));
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}

	public static void setStatus(String text) {
		lbStatus.setText(text);
	}

	private Label getLbCompany() {
		if (lbCompany == null) {
			lbCompany = new Label("ป้อนชื่อรหัสผ่าน");
			CompanyPopup.popup.setLabel(lbCompany);
			lbCompany.setStyleName("title");
			lbCompany
					.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			lbCompany.setPixelSize(600, 65);
			lbCompany.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					if (event.getNativeButton() == NativeEvent.BUTTON_LEFT)
						CompanyPopup.popup.open();
					// if (event.getNativeButton() == NativeEvent.BUTTON_MIDDLE)
					// {
					// if (Window.confirm("mode = " + mode + " change ? ")) {
					// mode = (mode + 1) % 3;
					// }
					// }
				}
			});
		}
		return lbCompany;
	}

	private AsyncCallback<Void> logoutCallback = new AsyncCallback<Void>() {
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
		}

		public void onSuccess(Void result) {
			closeBrowser();
		}
	};
	

	public native void closeBrowser()
	/*-{
		$wnd.closeWindow();
	}-*/;

	private Button getBtnBack() {
		if (btnBack == null) {
			btnBack = new Button("Back");
			btnBack.setStyleName("MenuButton");
			btnBack.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					MainPanel.mainPanel.back();
//					if (!MainPanel.mainPanel.back())
//						if (Window.confirm("ออกจากโปรแกรมหรือไม่?")) {
//							Library.getService().logout(logoutCallback);
//						}
				}
			});
			btnBack.setPixelSize(85, 65);
		}
		return btnBack;
	}

	public void refresh() {
		if (FormLogin.frmLogin.isConnected()) {
			CompanyPopup.popup.updateTitle();
			this.getBtnBack().setEnabled(true);
			MainMenu.menu.refresh();

		} else {
			lbCompany.setText("ป้อนชื่อผู้ใช้รหัสผ่าน");
			this.getBtnBack().setEnabled(false);
			MainPanel.mainPanel.reset();
		}
	}

	private AsyncCallback<String> userCallback = new AsyncCallback<String>() {
		public void onFailure(Throwable caught) {
		}

		public void onSuccess(String obj) {
			if (!FormLogin.frmLogin.isConnected()) {
				FormLogin.frmLogin.setUser(obj);
				if (obj == null) {
					FormLogin.frmLogin.enableLogin();
				} else {
					FormLogin.frmLogin.disableLogin();
				}
			}
			refresh();
		}
	};

}
